﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CDB.LinqModel.Data;
using CDB.LinqData;
using CDB.LinqModel;
using System.Web.UI.HtmlControls;

public partial class addBuilders : System.Web.UI.Page
{
    #region Variable Declaration
    IDataRepository _dataObject = new DataRepository();
    User user;
    Builder builder;
    List<Builder> builders = new List<Builder>();
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["User"] == null)
            Response.Redirect("default.aspx");
        else
        {
            if (Request.QueryString["bId"] != null)
                updateUserBuilder(Request.QueryString["bId"].ToString());
            bindBuilderGrid();
        }
    }
    private void updateUserBuilder(string builderId)
    {
        User nUser = new User(_dataObject);
        nUser.UserName = Session["User"].ToString();
        nUser.GetUserByUserName();

        if (nUser.Id != 0)
        {
            user = new User(_dataObject);
            user.Id = nUser.Id;
            user.BuilderId = Convert.ToInt32(builderId);
            user.UpdateBuilderId();
        }
    }
    protected void dListBuilders_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            HtmlGenericControl spDateCreated = (HtmlGenericControl)e.Item.FindControl("spDateCreated");
            Label spanDate = (Label)e.Item.FindControl("spanDate");

            if (!string.IsNullOrEmpty(spanDate.Text))
            {
                spDateCreated.InnerHtml = "<small>" +
                                          ((CDB.LinqModel.CDBGeneral.Month)Convert.ToDateTime(spanDate.Text).Month).
                                              ToString() + "</small>";
                spDateCreated.InnerHtml += "<div>" +
                                           (Convert.ToDateTime(spanDate.Text).Day).
                                               ToString() + "</div>";
            }
        }
    }
    private void bindBuilderGrid()
    {
        builder = new Builder(_dataObject);
        builders = builder.GetAllBuilders();
        if (builders.Count > 0)
        {
            spMessage.Text = string.Empty;
            dListBuilders.DataSource = builders;
            dListBuilders.DataBind();
        }
        else
        {
            spMessage.Text = "No Builders added.";
            dListBuilders.DataSource = null;
            dListBuilders.DataBind();
        }
    }
}